#pragma once

#include "Lifeline.h"

#include "ConnectableElement.h"
#include "Interaction.h"
#include "InteractionFragment.h"
#include "PartDecomposition.h"
#include "ValueSpecification.h"

namespace uml {

Lifeline::Lifeline(const QString& umlType)
    : NamedElement(umlType) {}

void Lifeline::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    NamedElement::fromJson(json, errorList);
    JsonSerializable::fromJsonArray(coveredBys_, "coveredBys", json, errorList);
    decomposedAs_ = JsonSerializable::create<PartDecomposition>(json.value("decomposedAs"), errorList);

    interaction_ = JsonSerializable::create<Interaction>(json.value("interaction"), errorList);

    represents_ = JsonSerializable::create<ConnectableElement>(json.value("represents"), errorList);

    selector_ = JsonSerializable::create<ValueSpecification>(json.value("selector"), errorList);

}

void Lifeline::toJson(QJsonObject& json) {
    NamedElement::toJson(json);
    JsonSerializable::toJsonArray(coveredBys_, "coveredBys", json);
    if (decomposedAs_) {
        QJsonObject decomposedAs_json;
        decomposedAs_->toJson(decomposedAs_json);
        json.insert("decomposedAs", decomposedAs_json);
    }
    if (interaction_) {
        QJsonObject interaction_json;
        interaction_->toJson(interaction_json);
        json.insert("interaction", interaction_json);
    }
    if (represents_) {
        QJsonObject represents_json;
        represents_->toJson(represents_json);
        json.insert("represents", represents_json);
    }
    if (selector_) {
        QJsonObject selector_json;
        selector_->toJson(selector_json);
        json.insert("selector", selector_json);
    }
}

/// <summary>
/// coveredBys getter
/// References the InteractionFragments in which this Lifeline takes part.
/// </summary>
vector<shared_ptr<InteractionFragment>>& Lifeline::coveredBys() {
    return coveredBys_;
}

/// <summary>
/// decomposedAs getter
/// References the Interaction that represents the decomposition.
/// </summary>
shared_ptr<PartDecomposition> Lifeline::decomposedAs() const {
    return decomposedAs_;
}

/// <summary>
/// decomposedAs setter
/// References the Interaction that represents the decomposition.
/// </summary>
void Lifeline::setDecomposedAs(shared_ptr<PartDecomposition> value) {
    decomposedAs_ = value;
}

/// <summary>
/// interaction getter
/// References the Interaction enclosing this Lifeline.
/// </summary>
shared_ptr<Interaction> Lifeline::interaction() const {
    return interaction_;
}

/// <summary>
/// interaction setter
/// References the Interaction enclosing this Lifeline.
/// </summary>
void Lifeline::setInteraction(shared_ptr<Interaction> value) {
    interaction_ = value;
}

/// <summary>
/// represents getter
/// References the ConnectableElement within the classifier that contains the
/// enclosing interaction.
/// </summary>
shared_ptr<ConnectableElement> Lifeline::represents() const {
    return represents_;
}

/// <summary>
/// represents setter
/// References the ConnectableElement within the classifier that contains the
/// enclosing interaction.
/// </summary>
void Lifeline::setRepresents(shared_ptr<ConnectableElement> value) {
    represents_ = value;
}

/// <summary>
/// selector getter
/// If the referenced ConnectableElement is multivalued, then this specifies the
/// specific individual part within that set.
/// </summary>
shared_ptr<ValueSpecification> Lifeline::selector() const {
    return selector_;
}

/// <summary>
/// selector setter
/// If the referenced ConnectableElement is multivalued, then this specifies the
/// specific individual part within that set.
/// </summary>
void Lifeline::setSelector(shared_ptr<ValueSpecification> value) {
    selector_ = value;
}

} // namespace uml
